package cn.tedu.supermarket.service.impl;

import cn.tedu.supermarket.entity.OrdersVO;
import cn.tedu.supermarket.entity.User;
import cn.tedu.supermarket.mapper.OrdersMapper;
import cn.tedu.supermarket.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Service
public class IOrdersServiceimpl implements IOrdersService {
    @Autowired
    OrdersMapper ordersMapper;

    @Override
    public Integer addOrders(int cartId,int addressId) {
        //获取user对象
        HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session=request.getSession();
        User user = (User)session.getAttribute("user");
        //补全goodsId,num,sumPrice
        OrdersVO ordersVO = ordersMapper.selectBycartId(cartId);
//        System.err.println(ordersVO);
        //获取price
        Integer price = ordersMapper.selectPriceByCartId(ordersVO.getGoodsId());
        //补全userId
        ordersVO.setUserId(user.getId());
        //补全addressId
        ordersVO.setAddressId(addressId);
        //补全price
        ordersVO.setPrice(price);
        //补全status
        String status = "未发货";
        ordersVO.setStatus(status);
//        System.err.println(ordersVO);
        Integer rows = ordersMapper.insertInfo(ordersVO);
        return rows;
    }
}
